package cn.tx.controller;

import cn.tx.service.impl.ZookeeperService;
import org.apache.zookeeper.CreateMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static org.apache.zookeeper.CreateMode.PERSISTENT;


/**
 * @author: 可乐加糖
 * @create: 2023-02-28 11:19
 * @Description: Zookeeper操作
 */
@RestController
public class ZookeeperApi {

    @Autowired
    private ZookeeperService zookeeperService ;

    /**
     * 获取节点下数据
     * @param path
     * @return
     */
    @GetMapping("/getNodeData")
    public String getNodeData (String path) {
        return zookeeperService.getNodeData(path) ;
    }

    /**
     * 判断节点是否存在
     * @param path
     * @return
     */
    @GetMapping("/isExistNode")
    public boolean isExistNode (final String path){
        return zookeeperService.isExistNode(path) ;
    }

    /**
     * 创建节点
     * @param path
     * @return
     */
    @GetMapping("/createNode")
    public String createNode (String path ){
        zookeeperService.createNode(CreateMode.PERSISTENT,path) ;
        return "success" ;
    }

    /**
     * 节点设置数据
     * @param path
     * @param nodeData
     * @return
     */
    @GetMapping("/setNodeData")
    public String setNodeData (String path, String nodeData) {
        zookeeperService.setNodeData(path,nodeData) ;
        return "success" ;
    }

    /**
     * 创建节点并保存数据
     * @param path
     * @param nodeData
     * @return
     */
    @GetMapping("/createNodeAndData")
    public String createNodeAndData ( String path , String nodeData){
        zookeeperService.createNodeAndData(PERSISTENT,path,nodeData) ;
        return "success" ;
    }

    /**
     *获取节点下数据
     * @param path
     * @return
     */
    @GetMapping("/getNodeChild")
    public List<String> getNodeChild (String path) {
        return zookeeperService.getNodeChild(path) ;
    }

    /**
     * 是否递归删除节点
     * @param path
     * @param recursive
     * @return
     */
    @GetMapping("/deleteNode")
    public String deleteNode (String path,Boolean recursive) {
        zookeeperService.deleteNode(path,recursive) ;
        return "success" ;
    }
}
